package com.ssm.controller;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ssm.bean.StockRecord;
import com.ssm.service.InOutStockRecordQueryService;
import com.ssm.util.Page;

/**
 * @author ZhaoXianming
 * @Description 入库管理	
 * @date 2020年3月12日
 */
@Controller
@RequestMapping(value = "/inOutStockRecordQuery")
public class InoutStockRecordController extends BaseController{

	Logger log = LoggerFactory.getLogger(InoutStockRecordController.class);
	
	@Autowired
	public InOutStockRecordQueryService inOutStockRecordQueryService;
	
	@RequestMapping(value = "/toMainJsp", method = RequestMethod.GET)
	public String toJsp(HttpServletRequest request){
		return "stockQuery/inoutStockRecordQuery";
	}
	
	/**
	 * @Description 查询出入库记录
	 * @param param
	 * @return Map<String,Object>
	 */
	/*@RequestMapping("/queryInOutRecord")
	@ResponseBody*/
	public Map<String,Object> query(@RequestBody StockRecord stockRecord, HttpServletRequest request){
		try{
			Map<String,Object> params = getPageParams(request);
			params.put("stockRecord", stockRecord);
			List<StockRecord> list = inOutStockRecordQueryService.queryInOutStockByParam(params);
			if(!list.isEmpty() && list.size() > 0){
				Page page = (Page) params.get("page");
				page.setTotalRecord(list.size());
			}
			params.put("materialList", list);
			return params;
		}catch(Exception e){
			log.error("查询出入库记录出错了！", e);
			return Collections.emptyMap();
		}
	}
	
	/**
	 * @Description 查询出入库记录
	 * @param param
	 * @return Map<String,Object>
	 */
	@RequestMapping("/queryInOutRecord/{pageNo}/{pageSize}")
	@ResponseBody
	public Map<String,Object> queryRecord(@RequestBody StockRecord stockRecord, @PathVariable int pageNo,
			@PathVariable int pageSize, HttpServletRequest request){
		try{
			Map<String,Object> params = new HashMap<String,Object>();
			params.put("stockRecord", stockRecord);
			params.put("pageNo", pageNo);
			params.put("pageSize", pageSize);
			int cnt = inOutStockRecordQueryService.queryInOutStockCnt(stockRecord);
			if(cnt > 0){
				List<StockRecord> list = inOutStockRecordQueryService.queryInOutStockByParam(params);
				if(!list.isEmpty() && list.size() > 0){
					params.put("totalPage",((int)Math.ceil((double)cnt/10)));
					params.put("totalRecord",cnt);
				}
				params.put("materialList", list);
			}
			return params;
		}catch(Exception e){
			log.error("查询出入库记录出错了！", e);
			return Collections.emptyMap();
		}
	}
	
	@RequestMapping("/isRefreshedJsp")
	@ResponseBody
	public int isRefreshedJsp(){
		return inOutStockRecordQueryService.isRefreshedJsp();
	}
}
